package gao.xiaolei.service;

import gao.xiaolei.dao.AuthVersionDao;
import gao.xiaolei.entity.AuthVersion;
import gao.xiaolei.util.RedisUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service("AuthVersionService")
public class AuthVersionService {

    @Resource(name = "AuthVersionDao")
    private AuthVersionDao authVersionDao;

    @Resource(name = "RedisUtil")
    private RedisUtil redis;

    //检验当前用户的权限版本号和网关储存的是否一致
    @Transactional
    public boolean versionCheck(String userId,Integer version){
        if(redis.hasKey(userId))//如果redis中有值
            return redis.get(userId).equals(version);
        else{//没有就去数据库里查找,查完要将值覆盖到redis
            AuthVersion av=authVersionDao.findById(userId).get();
            redis.set(userId,av.getVersion(),redis.EXPIRETIME);
            return version.equals(av.getVersion());
        }
    }
}
